package 贪心;

import java.util.*;

/**
 * @Author: biubiu0002
 * @Date: 2021-09-08 16:33
 * Leetcode 502
 **/
public class IPO {
    public static void main(String[] args) {
        int[] profits ={2,1,3};
        int[] capital = {1,0,2};
        int k=2;
        int w=0;
        System.out.println(findMaximizedCapital(k,w,profits,capital)); ;

    }

    public static int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
        int n = profits.length;
        int[][] arr = new int[n][2];
        for(int i=0;i<n;i++){
            arr[i][0]=profits[i];
            arr[i][1]=capital[i];
        }
        //按照captial升序排序
        // compare()返回>0 升序 <0 降序
        Arrays.sort(arr, (a,b)->a[1]-b[1]);
        for (int[] ints : arr) {
            System.out.println("profits="+ints[0]+"capital="+ints[1]);
        }
        //按照profits降序排序
        Queue<int[]> queue = new PriorityQueue<>((a,b)->b[0]-a[0]);
        int cur=0;
        for(int i=0;i<k;i++){
            //可做项目 按收益降序排序
            while(cur<n&&arr[cur][1]<=w){
                queue.add(arr[cur]);
                cur++;
            }
            w+=queue.poll()[0];
        }
        return w;

    }

}
